﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchFlight3 : System.Web.UI.Page
{
    FlightBAL _flightBAL;
    List<BigFlight> bigFlightListDepart;
    List<BigFlight> bigFlightListReturn;
    protected void Page_Load(object sender, EventArgs e)
    {
        _flightBAL = new FlightBAL();
        if (Session["DepartRoute"] == null || Session["DepartDates"] == null)
        {
            Response.Redirect("~/SearchFlight1.aspx");
        }
        if (!Page.IsPostBack)
        {
            Session["DepartFlights"] = null;
            Session["ReturnFlights"] = null;
        }
        List<DateTime> DepartDates = (List<DateTime>)Session["DepartDates"];
        BigRoute departRoute = Session["DepartRoute"] as BigRoute;
        int NumofPassenger = Convert.ToInt32(Session["Adult"]) + Convert.ToInt32(Session["Child"]) + Convert.ToInt32(Session["Senior"]);

        if (!Page.IsPostBack)
        {
            string text = departRoute.RouteList[0].Origin.FullName;
            foreach (SmallRoute route in departRoute.RouteList)
            {
                text += (" > " + route.Destination.FullName);
            }
            lblRoute.Text = text;


            List<Flight> firstFlights = new List<Flight>();
            foreach (DateTime date in DepartDates)
            {
                List<Flight> temp = _flightBAL.SearchFirstFlight(departRoute, date, NumofPassenger);
                if (temp != null)
                {
                    firstFlights.AddRange(temp);
                }
            }

            bigFlightListDepart = new List<BigFlight>();
            foreach (Flight flight in firstFlights)
            {
                BigFlight bigFligh = new BigFlight();
                List<Flight> flightList;
                if (departRoute.RouteList.Count > 1)
                {
                    flightList = _flightBAL.SearchNextFlights(flight, departRoute, NumofPassenger);
                }
                else
                {
                    flightList = new List<Flight>();
                    flightList.Add(flight);
                }
                if (flightList != null)
                {
                    bigFligh.FlightList = flightList;
                    bigFligh.DepartTime = flight.DepartTime;
                    bigFligh.Origin = flight.Route.Origin;
                    bigFligh.Destination = flightList[flightList.Count - 1].Route.Destination;

                    bigFlightListDepart.Add(bigFligh);
                }
            }

            if (bigFlightListDepart.Count > 0)
            {
                Session["bigFlightListDepart"] = bigFlightListDepart;
                for (int i = 0; i < bigFlightListDepart.Count; i++)
                {
                    ListItem item = new ListItem(bigFlightListDepart[i].DepartTime.ToString(), i.ToString());
                    DropDownList1.Items.Add(item);
                }
            }
            else
            {
                Session["bigFlightListDepart"] = new List<BigFlight>();
                lblNoavial1.Visible = true;
                DropDownList1.Enabled = false;
                Button1.Enabled = false;
            }
        }
        else
        {
            bigFlightListDepart = Session["bigFlightListDepart"] as List<BigFlight>;
        }
        DataList1.DataSource = bigFlightListDepart;
        DataList1.DataBind();


        if (Session["ReturnRoute"] != null || Session["ReturnDates"] != null)
        {

            if (!Page.IsPostBack)
            {
                PlaceHolder1.Visible = true;
                List<DateTime> ReturnDates = (List<DateTime>)Session["ReturnDates"];
                BigRoute ReturnRoute = Session["ReturnRoute"] as BigRoute;

                string text = ReturnRoute.RouteList[0].Origin.FullName;
                foreach (SmallRoute route in ReturnRoute.RouteList)
                {
                    text += (" > " + route.Destination.FullName);
                }
                lblReturnRoute.Text = text;

                List<Flight> firstFlightsReturn = new List<Flight>();
                foreach (DateTime date in ReturnDates)
                {
                    List<Flight> temp = _flightBAL.SearchFirstFlight(ReturnRoute, date, NumofPassenger);
                    if (temp != null)
                    {
                        firstFlightsReturn.AddRange(temp);
                    }
                }

                bigFlightListReturn = new List<BigFlight>();
                foreach (Flight flight in firstFlightsReturn)
                {
                    BigFlight bigFligh = new BigFlight();
                    List<Flight> flightList;
                    if (ReturnRoute.RouteList.Count > 1)
                    {
                        flightList = _flightBAL.SearchNextFlights(flight, ReturnRoute, NumofPassenger);
                    }
                    else
                    {
                        flightList = new List<Flight>();
                        flightList.Add(flight);
                    }

                    if (flightList != null)
                    {
                        bigFligh.FlightList = flightList;
                        bigFligh.DepartTime = flight.DepartTime;
                        bigFligh.Origin = flight.Route.Origin;
                        bigFligh.Destination = flightList[flightList.Count - 1].Route.Destination;

                        bigFlightListReturn.Add(bigFligh);
                    }
                }
                if (bigFlightListReturn.Count > 0)
                {
                    Session["bigFlightListReturn"] = bigFlightListReturn;
                    for (int i = 0; i < bigFlightListReturn.Count; i++)
                    {
                        ListItem item = new ListItem(bigFlightListReturn[i].DepartTime.ToString(), i.ToString());
                        DropDownList2.Items.Add(item);
                    }
                }
                else
                {
                    Session["bigFlightListReturn"] = new List<BigFlight>();
                    DropDownList2.Enabled = false;
                    lblNoavial2.Visible = true;
                    Button1.Enabled = false;
                }
            }
            else
            {
                bigFlightListReturn = Session["bigFlightListReturn"] as List<BigFlight>;
            }

            DataList3.DataSource = bigFlightListReturn;
            DataList3.DataBind();

        }
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            DataList1.SelectedIndex = e.Item.ItemIndex;
        }
        else if (e.CommandName == "UnSelect")
        {
            DataList1.SelectedIndex = -1;
        }
        DataList1.DataSource = bigFlightListDepart;
        DataList1.DataBind();
    }

    protected void DataList3_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            DataList3.SelectedIndex = e.Item.ItemIndex;
        }
        else if (e.CommandName == "UnSelect")
        {
            DataList3.SelectedIndex = -1;
        }
        DataList3.DataSource = bigFlightListReturn;
        DataList3.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        bigFlightListDepart = Session["bigFlightListDepart"] as List<BigFlight>;
        int index1 = Convert.ToInt32(DropDownList1.SelectedValue);
        Session["DepartFlights"] = bigFlightListDepart[index1];

        if (Session["ReturnRoute"] != null && Session["ReturnDates"] != null)
        {
            bigFlightListReturn = Session["bigFlightListReturn"] as List<BigFlight>;
            int index2 = Convert.ToInt32(DropDownList2.SelectedValue);
            Session["ReturnFlights"] = bigFlightListReturn[index2];
        }
        else
        {
            Session["ReturnFlights"] = null;
            Session["ReturnRoute"] = null;
            Session["ReturnDates"] = null;
        }

        Response.Redirect("~/SearchFlight4.aspx");
    }
}
